<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/22 0022
 * Time: 下午 4:03
 */

namespace Company\Controller;

use Think\Controller;

class AdController extends Controller
{

    public function getAd(){
        //  获取数据库广告信息
        $sid = $_SESSION['sid'];    //获取网站id

        $adSense = D("ad_sense");       //广告位
        $dateSense['sid']  = array('eq',$sid);
        $advert = $adSense->where($dateSense)->select();

        if ($advert){
            $ad = D("ad");
            $dateAd =array();
            foreach ($advert as $value){
                array_push($dateAd,$value['id']);    //获取新闻类id
            }

            $date['aid']  = array('in',$dateAd);
            $ad2 = $ad->where($date)->order('id desc')->select();    //  或去改网站的广告
            $adDate = array();
            foreach ($ad2 as $value){
                //改变aid的内容
                switch ($value['aid'])
                {
                    case 1;
                        $value['aid']="left";
                        break;
                    case 2;
                        $value['aid']="right";
                        break;
                }
                array_push($adDate,$value);     //把改变后的内容添加在一起
            }

            $this->assign("ad",$adDate);
            $this->display();

        }
        else{
            echo "<script>alert('用户无广告位信息');</script>";
        }

    }

    public function getAdFrom(){
        //  获取数据库广告信息
        $sid = $_SESSION['sid'];    //获取网站id

        $adSense = D("ad_sense");       //广告位

        $dateSense['sid']  = array('eq',$sid);
        $advert = $adSense->where($dateSense)->select();
        $position1 = array();
        $position = array();
        if ($advert){
            $i=1;
            foreach ($advert as $value){
                $position['position']=$value['position']."："."<input class='text' style='"."width:20px;"."' type='radio' name='position' value='".$i++."' />";
                array_push($position1,$position);
            }

            $this->assign("position",$position1);
            $this->display();
        }
        else{
            echo "<script>alert('用户无广告位信息');</script>";
        }

    }

    public function addAd(){

        //  接收表单数据
        $content=$_POST["content"];
        $type1=$_POST["type"];
        $position=$_POST["position"];

        $ad=D("ad");

        if($type1!=null and $content!=null and $position!=null){

            $data["content"]=$content;
            $data["type1"]=$type1;
            $data["aid"]=$position;
            if($_FILES['url']['error']!=4){
                if ($_FILES["url"]["error"]) {
                    $this->error('文件上传失败','getAd');
                }
                else {
                    if (file_exists("Public/upload/" . $_FILES["url"]["name"])) {
                        $this->error('文件名已存在','getAd');
                    }
                    else {
                        $data["url"] = $_FILES["url"]["name"];
                        $img = "Public/upload/" . $_FILES["url"]["name"];
                        move_uploaded_file($_FILES["url"]["tmp_name"], $img);
                    }
                }
            }

            $re=$ad->add($data);
            if(!empty($re)){
                $this->success('更改成功','getAd');
            }
            else{
                $this->error('更改失败','getAd');
            }
        }else{
            echo "内容不能为空！";
        }

    }

    public function upDateAd(){
        //  显示添加页面
        $ad=D("ad");

        $id=$_GET["id"];
        $list=$ad->where("id='$id'")->select();
        $aid = array();
        switch ($list[0]['aid'])
        {
            case 1;
                $aid[1]="checked";
                break;
            case 2;
                $aid[2]="checked";
                break;
        }

        //  获取数据库广告信息
        $sid = $_SESSION['sid'];    //获取网站id

        $adSense = D("ad_sense");       //广告位

        $dateSense['sid']  = array('eq',$sid);
        $advert = $adSense->where($dateSense)->select();
        $position1 = array();
        $position = array();
        if ($advert){
            $i=1;
            foreach ($advert as $value){
                $position['position']=$value['position']."：<input class='text' style='"."width:20px;"."' type='radio' name='position' value='".$i."'".$aid[$i]." />";
                array_push($position1,$position);
                $i++;
            }

            $this->assign("position",$position1);
        }
        else{
            echo "<script>alert('用户无广告位信息');</script>";
        }

        $this->assign("list",$list);
        $this->assign("id",$id);
        $this->display();
    }

    public function edAd(){

        //  接收表单数据
        $content=$_POST["content"];
        $type1=$_POST["type"];
        $position=$_POST["position"];
        $id = $_GET['id'];
        $ad=D("ad");

        if($type1!=null and $content!=null and $position!=null){

            $data["content"]=$content;
            $data["type1"]=$type1;
            $data["aid"]=$position;
            if (!empty($_FILES)){
                if($_FILES['url']['error']!=4){
                    var_dump($_FILES);
                    if ($_FILES["url"]["error"]) {
                        $this->error('文件上传失败','getAd');
                    }
                    else {
                        if (file_exists("Public/upload/" . $_FILES["url"]["name"])) {
                            $this->error('文件名已存在','getAd');
                        }
                        else {
                            $data["url"] = $_FILES["url"]["name"];
                            $img = "Public/upload/" . $_FILES["url"]["name"];
                            move_uploaded_file($_FILES["url"]["tmp_name"], $img);
                        }
                    }
                }
            }

            $re=$ad->where(['id'=>$id])->save($data);
            if(!empty($re)){
                $this->success('更改成功','getAd');
            }
            else{
                $this->error('更改失败','getAd');
            }
        }else{
            echo "内容不能为空！";
        }

    }

    public function deleteAd(){

        //  获取该广告id
        $id=$_GET["id"];
        //  根据id删除该行表格数据
        $ad=D("ad");
        $de = $ad->delete($id);
        if(!empty($de)){
            $this->success('删除成功','/Ad/getAd');
        }
        else{
            $this->error('删除失败','/Ad/getAd');
        }

    }

}